package com.spider.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.spider.entity.WanghuaEntity;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class WanghuaUtil {
    /**
     * 拼接新增产品信息
     * 编辑也使用此产品信息
     * @return
     */
    public static JSONObject convertProcudtBody(List<WanghuaEntity> list){
        List<WanghuaEntity> listNew=new  ArrayList<WanghuaEntity>();
        for(WanghuaEntity entity:list){
            double jbNum=Double.parseDouble(entity.getJibenNum());
            if(jbNum>1){
                entity.setSpecification(WanghuaUtil.deletePercent(entity.getJibenNum())+entity.getJibenunit());
                entity.setWeight(Integer.parseInt(WanghuaUtil.deletePercent(entity.getJibenNum())));
            }else{
                entity.setSpecification(WanghuaUtil.deletePercent(entity.getNum()+"")+entity.getUnit());
                entity.setWeight((int) entity.getNum());
            }
            //添加库存是否为0/0.00的判断
            int stock=Integer.parseInt(WanghuaUtil.deletePercent(entity.getStock()));
            if(stock==0){
                entity.setInstock(0);
                entity.setEtd_string("期货");
                entity.setShelf(0);
            }
            entity.setStockStr(entity.getStock());
            entity.setStock("国内("+WanghuaUtil.deletePercent(entity.getStock())+")");
            //检查中文名称/英文名称，如果同时包含两组括号 和-则跳过
            if(StringUtil.isNotEmpty(entity.getName())&&entity.getName().contains(")(")&&entity.getName().contains("-")){
                System.out.println(entity.getName());
                continue;
            }
            if(StringUtil.isNotEmpty(entity.getName_en())&&entity.getName_en().contains(")(")&&entity.getName_en().contains("-")){
                System.out.println(entity.getName());
                continue;
            }
            //修改价格
            BigDecimal base=new BigDecimal(entity.getPrice());
            BigDecimal discount=base;
            if(entity.getCxbs()!=null&&entity.getCxbs().equals("不打折")){
                //20230226 修改为不推送此类产品，改为下架
                entity.setInstock(0);
                entity.setEtd_string("期货");
                entity.setShelf(0);
                //discount=base.divide(new BigDecimal(0.8).setScale(2, BigDecimal.ROUND_HALF_UP));

            }else{
                //判断产品类型不是氘代的
                if(entity.getType()!=null&&entity.getType().equals("氘代")){
                    //20230226 修改为不推送此类产品，改为下架
                    entity.setInstock(0);
                    entity.setEtd_string("期货");
                    entity.setShelf(0);
                    //discount=base.divide(new BigDecimal(0.8).setScale(2, BigDecimal.ROUND_HALF_UP));
                }
            }
            entity.setPrice(discount.toString());
            listNew.add(entity);
        }
        JSONObject body=new JSONObject();
        if(list.size()==0)return null;
        JSONArray array= JSONArray.parseArray(JSON.toJSONString(listNew));
        body.put("product_list",array);
        System.out.println(body.toString());
        return body;
    }
    /**
     * 去除string小数
     * @param num
     * @return
     */
    public static String deletePercent(String num){
        if(StringUtil.isNotEmpty(num)){
            return num.split("\\.")[0];
        }
        return num;
    }

    /**
     * 插入数据的测试方法
     * @return
     */
    public static List getInsertListTest(){
        List<WanghuaEntity> list= new ArrayList<WanghuaEntity>();
        WanghuaEntity entity=new WanghuaEntity();
        entity.setShelf(1);
        entity.setJibenunit("g");
        entity.setJibenNum("5.0");
        entity.setUnit("g");
        entity.setBrand_name("希恩思");
        entity.setName("N-苯基苄脒");
        entity.setName_en("N-PHENYLBENZAMIDINE");
        entity.setProduct_no("N-021533+5g");
        entity.setHs("N-021533");
        entity.setType("0");
        entity.setCas("1527-91-9");
        entity.setCount(1);
        entity.setUnit("g");
        entity.setWeight(5);
        entity.setModel_specification("98%");
        entity.setSpecification("5g");
        entity.setPrice("185.0");
        entity.setInstock(1);
        entity.setStock("国内(9)");
        entity.setInstock(1);
        entity.setUpdate_time("2022-11-27 09:00:00");
        entity.setEtd_string("现货");
        entity.setProduct_type(0);
        entity.setPackage_method("瓶");
        list.add(entity);
        return list;
    }
    /**
     * 插入数据的测试方法
     * @return
     */
    public static List getInsertListTest2(){
        List<WanghuaEntity> list= new ArrayList<WanghuaEntity>();
        String str="{\"jibenunit\":\"g\",\"num\":250.0,\"hs\":\"A-0448506\",\"type\":\"0\",\"etd_string\":\"现货\",\"numDetail\":\"165.0000000000\",\"model_specification\":\"98%\",\"jibenNum\":\"0.25\",\"update_time\":\"2023-01-03 20:07:23.097\",\"cas\":\"13209-38-6\",\"price\":\"30.0\",\"package_method\":\"瓶\",\"stock\":\"国内(5)\",\"count\":1,\"specification\":\"250mg\",\"weight\":250,\"brand_name\":\"希恩思\",\"product_no\":\"A-0448506+0.25g\",\"shelf\":1,\"unit\":\"mg\",\"product_type\":0,\"stockStr\":\"5.00\",\"name\":\"(2-氨基苯基)(苯基)甲醇\",\"instock\":1,\"name_en\":\"(2-Aminophenyl)(phenyl)methanol\"}";
        WanghuaEntity jb=JSON.parseObject(str, WanghuaEntity.class);
        list.add(jb);
        return list;
    }
}
